package id.ac.amikom.amikomsocial;

import id.ac.amikom.libs.AdemDBHelper;
import id.ac.amikom.libs.AdemInet;
import id.ac.amikom.libs.AdemServiceHelper;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class AdemTabActivity extends TabActivity implements LocationListener {
	/** Called when the activity is first created. */

	protected LocationManager location;
	private ImageView user;
	private TextView title;
	private String imgUrl = "";
	private AdemDBHelper db = null;

	public class BackGroundTask extends AsyncTask<String, Void, Boolean> {

		@Override
		protected Boolean doInBackground(String... arg0) {
			AdemInet inet = new AdemInet();
			inet.downloadImage(imgUrl, "amikom_user.jpg");
			return null;
		}
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.adem_tab);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.adem_title);

		user = (ImageView) findViewById(R.id.icon);
		user.setImageResource(R.drawable.user_icon);

		title = (TextView) findViewById(R.id.title);
		title.setText("Users");

		user.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent i = new Intent(AdemTabActivity.this,
						AdemUserGroupActivity.class);								
				startActivity(i);

			}
		});
		
		Bundle extras = getIntent().getExtras();
		String img = extras.getString("url");
		if(!img.equals("")){
			imgUrl = extras.getString("url");
			new BackGroundTask().execute();
		}

		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;
		
		db = new AdemDBHelper(getApplicationContext());
		Cursor c = db.getLogin();
		
		if(c.getCount() > 0){
			intent = new Intent().setClass(this, AdemShoutActivity.class);
			spec = tabHost.newTabSpec("Shout")
					.setIndicator("Shout", res.getDrawable(R.drawable.ic_tab_shout))
					.setContent(intent);
			tabHost.addTab(spec);
		}

		intent = new Intent().setClass(this, AdemInfoActivity.class);
		spec = tabHost.newTabSpec("info")
				.setIndicator("Info", res.getDrawable(R.drawable.ic_tab_info))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AdemNewsActivity.class);
		spec = tabHost.newTabSpec("news")
				.setIndicator("News", res.getDrawable(R.drawable.ic_tab_news))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AdemCalendarActivity.class);
		spec = tabHost
				.newTabSpec("cald")
				.setIndicator("Cald",
						res.getDrawable(R.drawable.ic_tab_cald))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent().setClass(this, AdemMateriActivity.class);
		spec = tabHost
				.newTabSpec("materi")
				.setIndicator("File",
						res.getDrawable(R.drawable.ic_tab_materi))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);

		location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
				
		Location loc = location.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);		
		if (loc != null) {						
		}else{
			loc = location.getLastKnownLocation(LocationManager.GPS_PROVIDER);			
		}
		
		this.onLocationChanged(loc);

	}

	@Override
	protected void onResume() {
		super.onResume();
		location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*30, 1000, this);
		//location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*60*30, 1000, this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// location.removeUpdates(this);
	}

	public void onLocationChanged(Location loc) {

		double lat = 0;
		double lon = 0;

		if (loc != null) {
			lat = loc.getLatitude();
			lon = loc.getLongitude();
		}

		Geocoder geocoder = new Geocoder(this, Locale.getDefault());

		try {
			List<Address> addresses = geocoder.getFromLocation(lat, lon, 1);

			if (addresses != null && addresses.size() > 0) {
				Address returnedAddress = addresses.get(0);
				String addr = "";
				for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
					addr = addr + " " + returnedAddress.getAddressLine(i);
				}
				String location = addr.trim();
				AdemServiceHelper srv = new AdemServiceHelper(
						getApplicationContext());
				srv.updateActivity(location, lat, lon);

				Log.i("Location", location);

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}
}
